from typing import Any

import scrapy
from lxml import etree
from scrapy.http import Response
'''
 * @author siyu.swe
 * @version 1.0
 * {@code @date} 2024-04
'''
from 爬虫.eastmoney.eastmoney.items import quarterly_financial_report


class quarterly_financial_report_spider(scrapy.Spider):
    name = 'quarterly_financial_report'
    allowed_domains = ['emweb.securities.eastmoney.com']
    start_urls = [
        'https://emweb.securities.eastmoney.com/pc_hsf10/pages/index.html?type=web&code=SH688235&color=b#/cwfx']
    custom_settings = {
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.ConsolePipeline': 300},
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.MysqlPipeline': 200},
    }

    def parse(self, response):
        print('==============================')
        item = quarterly_financial_report()
        html = etree.HTML(response.text)
        print('==============================')
        item = quarterly_financial_report()
        html = etree.HTML(response.text)
        i = 2 #用于计算td[i] ---> 第几个表格 2开始是因为先导入了第一行
        title_num = html.xpath(
            '/html/body/div[1]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[1]/td')
        for i in range(len(title_num)):# len(title_num)表格有多长 作为循环条件-->表格一行有多少条数据
            item['basic_eps'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[2]/td[{i}]/text()')  # 基本每股收益
            item['deducted_eps'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[3]/td[{i}]/text()')  # 扣非每股收益
            item['diluted_eps'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[4]/td[{i}]/text()')  # 稀释每股收益
            item['net_assets_per_share'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[5]/td[{i}]/text()')  # 每股净资产
            item['per_share_provident_fund'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[6]/td[{i}]/text()')  # 每股公积金
            item['per_share_undistributed_profit'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[7]/td[{i}]/text()')  # 每股未分配利润
            item['per_share_operating_cash_flow'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[8]/td[{i}]/text()')  # 每股经营现金流
            item['growth_ability_indicator'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[9]/td[{i}]/text()')  # 成长能力指标
            item['total_revenue'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[10]/td[{i}]/text()')  # 营业总收入/html/body/div[1]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[10]/td[1]
            item['gross_profit'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[11]/td[{i}]/text()')  # 毛利润
            item['net_profit'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[12]/td[{i}]/text()')  # 归属净利润(元)
            item['deducted_net_profit'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[13]/td[{i}]/text()')  # 扣非净利润(元)
            item['revenue_growth_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[14]/td[{i}]/text()')  # 营业总收入同比增长( % )
            item['net_profit_growth_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[15]/td[{i}]/text()')  # 归属净利润同比增长( % )
            item['deducted_net_profit_growth_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[16]/td[{i}]/text()')  # 扣非净利润同比增长( % )
            item['revenue_rolling_growth_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[17]/td[{i}]/text()')  # 营业总收入滚动环比增长( % )
            item['net_profit_rolling_growth_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[18]/td[{i}]/text()')  # 归属净利润滚动环比增长( % )
            item['deducted_net_profit_rolling_growth_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[19]/td[{i}]/text()')  # 扣非净利润滚动环比增长( % )
            item['net_assets_return_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[21]/td[{i}]/text()')  # 净资产收益率(加权)( % )
            item['net_assets_return_rate_deducted'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[22]/td[{i}]/text()')  # 净资产收益率(扣非 / 加权)( % )
            item['total_assets_return_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[23]/td[{i}]/text()')  # 总资产收益率(加权)( % )
            item['gross_margin'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[24]/td[{i}]/text()')  # 毛利率( % )
            item['net_profit_margin'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[25]/td[{i}]/text()')  # 净利率( % )
            item['advances_to_accounts_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[27]/td[{i}]/text()')  # 预收账款 / 营业总收入
            item['sales_net_cashflow_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[28]/td[{i}]/text()')  # 销售净现金流 / 营业总收入
            item['operating_net_cashflow_rate'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[29]/td[{i}]/text()')  # 经营净现金流 / 营业总收入
            item['actual_taxation'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[30]/td[{i}]/text()')  # 实际税率( % )
            item['current_ratio'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[32]/td[{i}]/text()')  # 流动比率
            item['quick_ratio'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[33]/td[{i}]/text()')  # 速动比率
            item['cashflows_ratio'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[34]/td[{i}]/text()')  # 现金流量比率
            item['assetliabilities_ratio'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[35]/td[{i}]/text()')  # 资产负债率( % )
            item['equity_multiplier'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[36]/td[{i}]/text()')  # 权益系数
            item['equity_ratio'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[37]/td[{i}]/text()')  # 产权比率
            item['total_asset_turnover_days'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[39]/td[{i}]/text()')  # 总资产周转天数(天)
            item['inventory_turnover_days'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[40]/td[{i}]/text()')  # 存货周转天数(天)
            item['accounts_receivable_turnover_days'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[41]/td[{i}]/text()')  # 应收账款周转天数(天)
            item['total_asset_turnover_times'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[42]/td[{i}]/text()')  # 总资产周转率(次)
            item['inventory_turnover_times'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[43]/td[{i}]/text()')  # 存货周转率(次)
            item['accounts_receivable_turnover_times'] = html.xpath(
                f'//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[44]/td[{i}]/text()')  # 应收账款周转率(次)
            i+=1

            yield item
        # item['basic_eps'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[2]/td/text()')  # 基本每股收益
        # item['deducted_eps'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[3]/td/text()')  # 扣非每股收益
        # item['diluted_eps'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[4]/td/text()')  # 稀释每股收益
        # item['net_assets_per_share'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[5]/td/text()')  # 每股净资产
        # item['per_share_provident_fund'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[6]/td/text()')  # 每股公积金
        # item['per_share_undistributed_profit'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[7]/td/text()')  # 每股未分配利润
        # item['per_share_operating_cash_flow'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[8]/td/text()')  # 每股经营现金流
        # item['growth_ability_indicator'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[9]/td/text()')  # 成长能力指标
        # item['total_revenue'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[10]/td/text()')  # 营业总收入
        # item['gross_profit'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[11]/td/text()')  # 毛利润
        # item['net_profit'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[12]/td/text()')  # 归属净利润(元)
        # item['deducted_net_profit'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[13]/td/text()')  # 扣非净利润(元)
        # item['revenue_growth_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[14]/td/text()')  # 营业总收入同比增长( % )
        # item['net_profit_growth_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[15]/td/text()')  # 归属净利润同比增长( % )
        # item['deducted_net_profit_growth_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[16]/td/text()')  # 扣非净利润同比增长( % )
        # item['revenue_rolling_growth_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[17]/td/text()')  # 营业总收入滚动环比增长( % )
        # item['net_profit_rolling_growth_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[18]/td/text()')  # 归属净利润滚动环比增长( % )
        # item['deducted_net_profit_rolling_growth_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[19]/td/text()')  # 扣非净利润滚动环比增长( % )
        # item['net_assets_return_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[21]/td/text()')  # 净资产收益率(加权)( % )
        # item['net_assets_return_rate_deducted'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[22]/td/text()')  # 净资产收益率(扣非 / 加权)( % )
        # item['total_assets_return_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[23]/td/text()')  # 总资产收益率(加权)( % )
        # item['gross_margin'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[24]/td/text()')  # 毛利率( % )
        # item['net_profit_margin'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[25]/td/text()')  # 净利率( % )
        # item['advances_to_accounts_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[27]/td/text()')  # 预收账款 / 营业总收入
        # item['sales_net_cashflow_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[28]/td/text()')  # 销售净现金流 / 营业总收入
        # item['operating_net_cashflow_rate'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[29]/td/text()')  # 经营净现金流 / 营业总收入
        # item['actual_taxation'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[30]/td/text()')  # 实际税率( % )
        # item['current_ratio'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[32]/td/text()')  # 流动比率
        # item['quick_ratio'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[33]/td/text()')  # 速动比率
        # item['cashflows_ratio'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[34]/td/text()')  # 现金流量比率
        # item['assetliabilities_ratio'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[35]/td/text()')  # 资产负债率( % )
        # item['equity_multiplier'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[36]/td/text()')  # 权益系数
        # item['equity_ratio'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[37]/td/text()')  # 产权比率
        # item['total_asset_turnover_days'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[39]/td/text()')  # 总资产周转天数(天)
        # item['inventory_turnover_days'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[40]/td/text()')  # 存货周转天数(天)
        # item['accounts_receivable_turnover_days'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[41]/td/text()')  # 应收账款周转天数(天)
        # item['total_asset_turnover_times'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[42]/td/text()')  # 总资产周转率(次)
        # item['inventory_turnover_times'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[43]/td/text()')  # 存货周转率(次)
        # item['accounts_receivable_turnover_times'] = html.xpath(
        #     '//*[@id="app"]/div[4]/div/div[1]/div[2]/div[4]/table[1]/tbody/tr[44]/td/text()')  # 应收账款周转率(次)
        # yield item
